import { Linking } from 'react-native'
import { IS_ANDROID } from './ScreenUtil'

export const NAVIGSTE_MAP = {
  BAIDU: { key: 'baidu', name: '百度地图' },
  AMAP: { key: 'amap', name: '高德地图' },
  QQMAP: { key: 'qqmap', name: '腾讯地图' }
}
export const isInstallAmap = () => {
  return new Promise((resolve) => {
    Linking.canOpenURL(IS_ANDROID ? 'amapuri://route/plan/' : 'iosamap://path').then(supported => {
      resolve({
        name: NAVIGSTE_MAP.AMAP.name,
        supported: supported,
        navigate: openAmap
      })
    }).catch(() => {
      resolve({
        supported: false
      })
    })
  })
}

export const isInstallBaidumap = () => {
  return new Promise((resolve) => {
    Linking.canOpenURL('baidumap://map/direction').then(supported => {
      resolve({
        name: NAVIGSTE_MAP.BAIDU.name,
        supported: supported,
        navigate: openBaiduMap
      })
    }).catch(() => {
      resolve({
        supported: false
      })
    })
  })
}
export const isInstallQQmap = () => {
  return new Promise((resolve) => {
    Linking.canOpenURL('qqmap://map/routeplan').then(supported => {
      resolve({
        name: NAVIGSTE_MAP.QQMAP.name,
        supported: supported,
        navigate: openQQMap
      })
    }).catch(() => {
      resolve({
        supported: false
      })
    })
  })
}

export const isInstallMap = () => {
  return Promise.all([isInstallAmap(), isInstallBaidumap(), isInstallQQmap()])
}
/**
 * 打开高德地图导航
 * @param {String} data.sname - 起点名字.
 * @param {String|number} data.slon - 起点经度.
 * @param {String|number} data.slat - 起点纬度.
 * @param {String} data.dname - 终点名字.
 * @param {String|number} data.dlon - 终点经度.
 * @param {String|number} data.dlat - 终点纬度.
 * @param{Mode} data.mode 导航类型
 * @param data
 */
export const openAmap = (data = {}) => {
  let base = IS_ANDROID ? 'amapuri://route/plan/?' : 'iosamap://path?'
  return new Promise((resolve, reject) => {
    // 起点经纬度不传，则自动将用户当前位置设为起点
    if (!data.dlat || !data.dlon) {
      resolve('需要终点经纬度')
    } else {
      if (data.slon && data.slat) {
        base += `&slat=${data.slat}&slon=${data.slon}`
      }
      if (data.sname) {
        base += `&sname=${data.sname}`
      }
      if (data.dname) {
        base += `&dname=${data.dname}`
      }
      base += `&dlat=${data.dlat}&dlon=${data.dlon}&dev=0&t=${data.mode ? (data.mode.amap || 0) : 0}`
      console.info(base)
      Linking.openURL(base).then(res => {
      }).catch(err => {
        reject('暂无安装高德地图')
      })
    }
  })
}
export const openBaiduMap = (data = {}) => {
  const appUrl = `baidumap://map/direction?destination=${data.dlat},${data.dlon}&mode=driving&destination=${data.dname}&coord_type=gcj02`
  Linking.openURL(appUrl)
}

export const openQQMap = (data = {}) => {
  const appUrl = `qqmap://map/routeplan?type=drive&to=${data.dname}&tocoord=${data.dlat},${data.dlon}&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77`
  Linking.openURL(appUrl)
}
export const openAppleMap = (data = {}) => {
  const appUrl = `http://maps.apple.com/?ll=${data.dlat},${data.dlon}&q=${data.dname}`
  Linking.openURL(appUrl)
}
